/* eslint-disable */
export function downExcel (head, datas) {
  require.ensure([], () => {
    const {exportJsonToExcel} = require('@/vendor/Export2Excel')
    let tHeader = []
    let filterVal = []
    for (var i in head) {
      tHeader.push(head[i]['text'])
      filterVal.push(head[i]['value'])
    }
    const list = datas
    const data = formatJson(filterVal, list)
    exportJsonToExcel(tHeader, data, '列表excel')
  })
}

function formatJson (filterVal, jsonData) {
  return jsonData.map(v => filterVal.map(j => v[j]))
}

// 以下为格式化显示json字符串的代码
function repeat (s, count) {
  return new Array(count + 1).join(s)
}

function formatJsonPage (json) {
  var i = 0,
    il = 0,
    tab = '',
    newJson = '',
    indentLevel = 0,
    inString = false,
    currentChar = null
  for (i = 0, il = json.length; i < il; i += 1) {
    currentChar = json.charAt(i)
    switch (currentChar) {
      case '{':
      case '[':
        if (!inString) {
          newJson += currentChar + '\n' + repeat(tab, indentLevel + 1)
          indentLevel += 1
        } else {
          newJson += currentChar
        }
        break
      case '}':
      case ']':
        if (!inString) {
          indentLevel -= 1
          newJson += '\n' + repeat(tab, indentLevel) + currentChar
        } else {
          newJson += currentChar
        }
        break
      case ',':
        if (!inString) {
          newJson += ',\n' + repeat(tab, indentLevel)
        } else {
          newJson += currentChar
        }
        break
      case ':':
        if (!inString) {
          newJson += ': '
        } else {
          newJson += currentChar
        }
        break
      case ' ':
      case '\n':
      case '\t':
        if (inString) {
          newJson += currentChar
        }
        break
      case '"':
        if (i > 0 && json.charAt(i - 1) !== '\\') {
          inString = !inString
        }
        newJson += currentChar
        break
      default:
        newJson += currentChar
        break
    }
  }
  return newJson
}

export function getFormatData (json) {
  var json = json + ''
  if (json.indexOf('{') == -1 && json.indexOf('[') == -1) {
    return json
  } else {
    return (formatJsonPage(json))
  }
}
